package models

import (
	"OPS_TEMPLATE/util"
	"github.com/bitly/go-simplejson"
	"strconv"
	"strings"
)

type Tables struct {
	Id          int    `pk;column(id);auto"`
	Name        string `orm:"size(50); column(name)"`
	Description string `orm:"size(50); column(description)"`
	Code        string `orm:"size(50); column(code)"`
	Sql         string `orm:"column(sql);type(text)"`
	Head        string `orm:"size(50); column(head)"`
	Mount       string `orm:"column(mount)"`
	Button      string `orm:"column(button);type(text)"`
	Col         string `orm:"column(col)"`
}

//验证
func (this *Tables) Valid() (res bool, errorm map[string]string) {
	errormap := make(map[string]string)
	var sql string
	var sql2 string
	bl := true
	if this.Id != 0 {
		sql = "SELECT * FROM tables WHERE name =\"" + this.Name + "\" and id <> " + strconv.Itoa(this.Id)
		sql2 = "SELECT * FROM tables WHERE code =\"" + this.Code + "\" and id <> " + strconv.Itoa(this.Id)

	} else {
		sql = "SELECT * FROM permissions WHERE name =\"" + this.Name + "\" "
		sql2 = "SELECT * FROM permissions WHERE code =\"" + this.Code + "\" "
	}
	o1 := GetOrm()
	var mm []Tables

	num, err := o1.Raw(sql).QueryRows(&mm)
	if err == nil {
		if num == 0 {
			bl = true
		} else {
			bl = false
			errormap["errName"] = "名字不能重复"
		}
	}

	num, err = o1.Raw(sql2).QueryRows(&mm)
	if err == nil {
		if num == 0 {
			bl = true

		} else {
			bl = false
			errormap["errCode"] = "编码不能重复"
		}
	}

	if strings.TrimSpace(this.Name) == "" {
		errormap["errName"] = "名字不能为空"
		bl = false
	}

	if strings.TrimSpace(this.Code) == "" {
		errormap["errCode"] = "编码不能为空"
		bl = false
	}

	if strings.TrimSpace(this.Head) == "" {
		errormap["errHead"] = "列头不能为空"
		bl = false
	}

	if strings.TrimSpace(this.Col) == "" {
		errormap["errCol"] = "列头对应sql不能为空"
		bl = false
	}

	if len(strings.Split(this.Col, ",")) != len(strings.Split(this.Head, ",")) {
		errormap["errHead"] = "列头跟对应的sql长度不一致"
		bl = false
	}

	if !util.IsNum(this.Mount) {
		errormap["errMount"] = "必须为数字"
		bl = false
	} else {
		if this.Mount == "0" {
			errormap["errMount"] = "不能为0"
			bl = false
		}
	}

	if strings.TrimSpace(this.Sql) != "" {
		sql = "select * from ( " + this.Sql + ") zzzz limit 0,1"
		_, err = o1.Raw(sql).Exec()
		miserror := "right syntax to use near '?"

		if err != nil && !strings.Contains(err.Error(), miserror) {
			errormap["errSql"] = "SQL语句执行错误"
			bl = false
		}
	} else {
		errormap["errSql"] = "SQL语句不能为空"
		bl = false
	}

	if strings.TrimSpace(this.Button) != "" {
		js, err := simplejson.NewJson([]byte(this.Button))
		if err != nil {
			errormap["errButton"] = "json格式有误"
			bl = false
		} else {
			a, err := js.Get("Button").Array()
			if err != nil {
				errormap["errButton"] = "json格式有误,缺少Button字段"
				bl = false
			} else {
				for i := 0; i < len(a); i++ {
					_, err = js.Get("Button").GetIndex(i).Get("Name").String()
					if err != nil {
						errormap["errButton"] = "json格式有误,Button数组下面的Name字段不能为空"
						bl = false
					}
				}
			}

		}
	} else {
		errormap["errButton"] = "json字段不能为空"
		bl = false
	}

	return bl, errormap
}
